package com.wenwei.sharebox.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.bean.RegisterBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.AccountValidatorUtil;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func3;
import rx.functions.Func4;

/**
 * Created by Administrator on 2017/7/13.
 */
public class RegisterActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.register_user_head)
    CircleImageView registerUserHead;
    @Bind(R.id.input_phone)
    EditText inputPhone;
    @Bind(R.id.input_pw)
    EditText inputPw;
    @Bind(R.id.input_code)
    EditText inputCode;
    @Bind(R.id.register_get_code)
    TextView registerGetCode;
    @Bind(R.id.commit_btn)
    TextView commitBtn;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_register;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("注册");

        Observable<CharSequence> phoneObservable = RxTextView.textChanges(inputPhone);
        Observable<CharSequence> psObservable = RxTextView.textChanges(inputPw);
        Observable<CharSequence> codeObservable = RxTextView.textChanges(inputCode);

        Observable.combineLatest(phoneObservable, psObservable, codeObservable, new Func3<CharSequence, CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3) {
                return !charSequence.toString().equals("") && !charSequence2.toString().equals("")
                        && !charSequence3.toString().equals("") && AccountValidatorUtil.isMobile(charSequence.toString());
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                commitBtn.setEnabled(aBoolean);
                if(aBoolean){
                    commitBtn.setBackgroundResource(R.drawable.bc_share_select);
                }else{
                    commitBtn.setBackgroundResource(R.drawable.bc_click_false);
                }
            }
        });

    }

    @OnClick({R.id.base_back, R.id.register_user_head, R.id.register_get_code, R.id.commit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.register_user_head:
                break;
            case R.id.register_get_code:
                break;
            case R.id.commit_btn:
                userRegister();
                break;
        }
    }


    BaseSubscriber<RegisterBean> registerSubscriber;
    private void userRegister(){
        String userPhone = inputPhone.getText().toString();
        String userPw = inputPw.getText().toString();
        String userCode = inputCode.getText().toString();
        registerSubscriber = new BaseSubscriber<RegisterBean>(mContext) {
            @Override
            public void onSuccess(RegisterBean registerBean) {
                showToast("注册成功，快来体验吧");
                setResult(RESULT_OK);
                finish();
            }
        };
        try{
            JSONObject jb = new JSONObject();
            jb.put("password",userPw);
            jb.put("username",userPhone);
            ApiManager.getInstance().userRegister(registerSubscriber,userPhone,userPw);
        }catch (Exception e){}
    }
}
